package com.ele.client.words;

import com.ele.client.Presenter;
import com.ele.client.rpc.ApplicationServiceAsync;
import com.ele.client.rpc.SimpleAsyncCallback;
import com.ele.client.util.PathHelper;
import com.ele.model.Word;
import com.github.gwtbootstrap.client.ui.Button;
import com.github.gwtbootstrap.client.ui.TextBox;
import com.google.gwt.event.dom.client.*;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;

public class EditorPresenter implements Presenter {

    public interface Display {
        Widget asWidget();

        TextBox getWordTextBox();

        TextBox getTranslationTextBox();

        Button getSaveButton();

        Button getCancelButton();

        Button getNextButton();

        Button getPrevButton();
    }

    private final ApplicationServiceAsync rpcService;
    private final HandlerManager eventBus;
    private final Display display;

    private Word value;

    public EditorPresenter(ApplicationServiceAsync rpcService, HandlerManager eventBus, Display display) {
        this.rpcService = rpcService;
        this.eventBus = eventBus;
        this.display = display;
    }

    @Override
    public void go(HasWidgets container) {
        bind();
        container.clear();
        container.add(display.asWidget());

        Long id = PathHelper.getId();
        if (id != null) {
            loadWord(id);
        } else {
            Word word = new Word();
            setValue(word);
        }

//        display.getWordTextBox().setFocus(true);
        display.getTranslationTextBox().setFocus(true);
    }

    private void bind() {
        display.getSaveButton().addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                save();
            }
        });

        display.getCancelButton().addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {

            }
        });

        display.getPrevButton().addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                goToPrev();
            }
        });

        display.getNextButton().addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                goToNext();
            }
        });

        display.getTranslationTextBox().addKeyDownHandler(new KeyDownHandler() {
            @Override
            public void onKeyDown(KeyDownEvent event) {
                if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
                    save();
                }
            }
        });
    }

    private void save() {
        Word value = getValue();

        display.getSaveButton().state().loading();
        rpcService.saveWord(value, new SimpleAsyncCallback<Word>() {
            @Override
            public void onSuccess(Word word) {
                display.getSaveButton().state().reset();
                setValue(word);

                goToNext();
            }
        });
    }

    private void goToNext() {
        Long id = value.getId();
        if (id != null) {
            History.newItem("action=word-editor&id=" + (id + 1));
        }
    }

    private void goToPrev() {
        Long id = value.getId();
        if (id != null) {
            History.newItem("action=word-editor&id=" + (id - 1));
        }
    }

    private void loadWord(Long id) {
        rpcService.getWord(id, new SimpleAsyncCallback<Word>() {
            @Override
            public void onSuccess(Word word) {
                if (word == null) {
                    word = new Word();
                }
                setValue(word);
            }
        });
    }

    public void setValue(Word word) {
        value = word;

        display.getWordTextBox().setValue(word.getText());
        display.getTranslationTextBox().setValue(word.getTranslation());

        PathHelper.setId(word.getId());
    }

    private Word getValue() {
        value.setText(display.getWordTextBox().getValue());
        value.setTranslation(display.getTranslationTextBox().getValue());

        return value;
    }
}
